import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Notification } from './entities/notification.entity';
import { Repository } from 'typeorm';
import { User } from 'src/user/entities/user.entity';

@Injectable()
export class NotificationService {
  @InjectRepository(Notification)
  private readonly notificationRepository: Repository<Notification>;
  @InjectRepository(User)
  private readonly userRepository: Repository<User>;

  async sendEnterpriseNotification(message: string, userIds: number[]) {
    console.log(`服务器推送消息: ${message}`);

    const notifications = userIds.map((userId) =>
      this.notificationRepository.create({
        content: message,
        user: { id: userId }, // 直接赋值外键
      }),
    );

    // 批量保存通知
    await this.notificationRepository.save(notifications);
    return `服务器推送消息: ${message} (已通知 ${userIds.length} 个用户)`;
  }
}
